<template>
  <div class="zc-select-modal">
    <div class="zc-select-wrap">
      <div class="title-wrap flx-justify-between">
        <div class="title">{{ title }}</div>
        <div class="close pointer" @click="close">
          <el-icon>
            <Close/>
          </el-icon>
        </div>
      </div>

      <div class="zc-select-content flx-justify-between">
        <div class="left">
          <el-input v-model="searchKeywords" placeholder="请输入姓名进行搜索">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Search/>
              </el-icon>
            </template>
          </el-input>

          <div class="select-card">
            <div class="sub-title">选择角色</div>
            <div class="right-member">
              <el-scrollbar class="scrollbar">
                <el-checkbox
                  v-if="multiple && memberList.length"
                  v-model="checkAll"
                  :indeterminate="isIndeterminate"
                  @change="handleCheckAllChange"
                >
                  全选
                </el-checkbox>
                <el-checkbox-group v-model="checkedMember" style="display: grid;" @change="handleCheckedMemberChange">
                  <el-checkbox
                    v-for="(member, index) in memberList"
                    :key="index"

                    :label="member.id"
                    :value="member.id"
                  >
                    {{ member.name }}
                  </el-checkbox>
                </el-checkbox-group>

                <el-empty v-if="!memberList || !memberList.length" description="未搜索到相关人员"></el-empty>
              </el-scrollbar>
            </div>
          </div>
        </div>
        <div class="right">
          <div class="right-top flx-justify-between">
            <div class="selected text-color-6">
              已选(<span class="text-color-8">{{ checkedMember.length }}</span
            ><span class="text-color-4">/{{ multiple ? 100 : 1 }}</span
            >)
            </div>
            <div class="text-primary pointer" @click="emptied">清空</div>
          </div>
          <el-scrollbar class="flx-1">
            <div class="selected-list">
              <div v-for="(item, index) in checkedMember" :key="index" class="item text-color-6 flx-align-center">
                {{ findeMemberName(item) }}
                <el-icon @click="delMember(index)">
                  <Close/>
                </el-icon>
              </div>
            </div>
          </el-scrollbar>
          <div class="footer flx-justify-end">
            <el-button @click="close">取消</el-button>
            <el-button v-throttle="submit" :disabled="!checkedMember.length" type="primary">确定</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {Close, Search} from "@element-plus/icons-vue";
import {computed, defineEmits, defineProps, ref} from "vue";

const emit = defineEmits(["close", "selectRole"]);
const prop = defineProps({
  // 标题
  title: {
    type: String,
    default: "选择角色"
  },

  // 是否多选
  multiple: {
    type: Boolean,
    default: true
  }
});

// 搜索关键词
const searchKeywords = ref("");
const currentTab = ref(0);
const tabList = ["角色", "部门", "用户"];
// 角色列表
const roleList = ref([
  {id: 1, name: "销售总监"},
  {id: 2, name: "知产顾问"},
  {id: 3, name: "销售组长"},
  {id: 4, name: "推广主管"},
  {id: 5, name: "知产经理"},
  {id: 6, name: "知产材料员"},
  {id: 7, name: "知产主管"}
]);

// 切换角色 部门和用户
const tabsChange = index => {
  currentTab.value = index;
};

// 已选中的角色
const currentRole = ref("");

// 部门列表
const data = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1"
          }
        ]
      }
    ]
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1"
          }
        ]
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1"
          }
        ]
      }
    ]
  },
  {
    label: "Level one 3",
    children: [
      {
        label: "Level two 3-1",
        children: [
          {
            label: "Level three 3-1-1"
          }
        ]
      },
      {
        label: "Level two 3-2",
        children: [
          {
            label: "Level three 3-2-1"
          }
        ]
      }
    ]
  }
];

// 选择部门
const handleCheckChange = (data, checked, indeterminate) => {
  console.log(data, checked, indeterminate);
};

// 用户列表
const memberList = ref([
  {id: 1, name: "销售总监"},
  {id: 2, name: "知产顾问"},
  {id: 3, name: "销售组长"},
]);

// 已选中的人员
const checkedMember = ref([]);
// 所有人员的id
const memberIds = [1, 2, 3, 4, 5, 6, 7];

// 是否全选
const checkAll = ref(false);
// 是否半选
const isIndeterminate = ref(false);

// 全选
const handleCheckAllChange = (val: boolean) => {
  checkedMember.value = val ? JSON.parse(JSON.stringify(memberIds)) : [];
  isIndeterminate.value = false;
};

// 单选
const handleCheckedMemberChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === memberIds.length;
  isIndeterminate.value = checkedCount > 0 && checkedCount < memberIds.length;
};

// 通过id 查找对应的用户名
const findeMemberName = computed(() => {
  return (id: string) => {
    return memberList.value.find((item: any) => item.id == id).name;
  };
});

// 清空
const emptied = () => {
  checkAll.value = false;
  isIndeterminate.value = false;
  checkedMember.value = [];
};
// 删除已选
const delMember = index => {
  checkedMember.value.splice(index, 1);
  if (checkedMember.value.length == 0) {
    isIndeterminate.value = false;
    console.log(isIndeterminate.value);
  } else {
    checkAll.value = false;
    isIndeterminate.value = true;
  }
};

// 关闭
const close = () => {
  emit("close");
};

// 确定
const submit = () => {
  emit("selectRole", checkedMember.value);
};
</script>

<style lang="scss" scoped>
.zc-select-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;

  .zc-select-wrap {
    width: 600px;
    height: 530px;
    padding: 11px 20px 20px 20px;
    box-sizing: border-box;
    box-shadow: 0px 16px 48px 16px rgba(0, 0, 0, 0.08),
    0px 12px 32px 0px rgba(0, 0, 0, 0.12),
    0px 8px 16px -8px rgba(0, 0, 0, 0.16);
    background: #ffffff;
    border-radius: 4px;

    .title-wrap {
      height: 26px;
      line-height: 26px;

      .title {
        font-size: 18px;
        font-weight: 700;
      }
    }

    .zc-select-content {
      padding-top: 11px;
      align-items: flex-start;

      .left {
        width: 372px;

        .select-card {
          border: 1px solid rgba(186, 191, 197, 0.4);
          border-radius: 4px;
          padding: 8px;
          height: 404px;
          margin-top: 10px;
          display: flex;
          flex-direction: column;

          .sub-title {
            height: 32px;
            line-height: 32px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
            margin-bottom: 8px;
          }


          .right-member {
            flex: 1;
            margin-left: 18px;

            .el-checkbox {
              height: 36px;
              width: 100%;
              margin: 0;
            }
          }
        }
      }

      .right {
        height: 464px;
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 20px;

        .right-top {
          height: 22px;
          padding: 5px 0;
          border-bottom: 1px solid rgba(186, 191, 197, 0.4);
        }

        .selected-list {
          display: flex;
          flex-wrap: wrap;
          margin-left: -8px;
          margin-top: 10px;

          .item {
            height: 30px;
            background: #f5f6f7;
            border-radius: 4px;
            padding: 0 11px 0 8px;
            margin-left: 8px;
            margin-bottom: 8px;
            cursor: pointer;

            i {
              vertical-align: middle;
              margin-top: 2px;
              margin-left: 8px;
            }
          }
        }
      }
    }
  }
}
</style>
